<template>
  <div class="search-components">
    <div class="search-component">
      <div class="row keyword">
        <p class="label">关键词</p>
        <el-input
          v-model="searchModel.name"
          style="width: 220px"
          placeholder="卡名称"
          @keyup.enter="handleSearch"
        />
      </div>
      <div class="row keyword">
        <p class="label">卡类型</p>
        <el-select v-model="searchModel.type" style="width: 220px" placeholder="卡状态">
          <el-option label="全部" value="" />
          <el-option
            v-for="item in typeList.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <div class="row keyword">
        <p class="label">线上办理</p>
        <el-select
          v-model="searchModel.allowOnline"
          style="width: 220px"
          placeholder="卡状态"
        >
          <el-option label="全部" value="" />
          <el-option
            v-for="item in allowList.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <div class="row">
        <p class="label">启用状态</p>
        <el-select v-model="searchModel.status" style="width: 220px" placeholder="卡状态">
          <el-option
            v-for="item in statusList.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <div class="search-btn">
        <el-button class="reset-btn" type="primary" @click="handleReset">
          <img class="icon-img" :src="iconReset" alt="" />
          重置</el-button
        >
        <el-button class="submit-btn" type="primary" @click="handleSearch">
          <img class="icon-img" :src="iconSearch" alt="" />
          查询</el-button
        >
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import iconSearch from "@/assets/image/btn/icon-search.png";
import iconReset from "@/assets/image/btn/icon-reset.png";

const emits = defineEmits(["search"]);

const props = defineProps({
  listData: {
    type: Object,
    default: () => {},
  },
});

console.log("props ===>", props);

const searchModel = ref({});

const handleReset = () => {
  searchModel.value = {};
  emits("search", searchModel.value);
};

const handleSearch = () => {
  console.log(`output-> searchModel.value`, searchModel.value);
  emits("search", searchModel.value);
};

const typeList = reactive({
  data: [
    {
      id: 1,
      name: "月卡",
    },
    {
      id: 2,
      name: "季卡",
    },
    {
      id: 3,
      name: "年卡",
    },
  ],
});

const allowList = reactive({
  data: [
    {
      id: 1,
      name: "允许",
    },
    {
      id: 0,
      name: "不允许",
    },
  ],
});

const statusList = reactive({
  data: [
    {
      id: "",
      name: "全部",
    },
  ],
});

onMounted(() => {
  setTimeout(() => {
    if (
      props.listData.statusList.length > 0 &&
      props.listData.statusList[0].name !== "全部"
    ) {
      // 如果不是，将“全部”选项添加到props.statusList的开头
      statusList.data = [...statusList.data, ...props.listData.statusList];
    } else {
      statusList.data = props.listData.statusList;
    }
  }, 1000);
});
</script>

<style lang="less" scoped>
.search-components {
  // display: flex;
  // justify-content: space-between;
  .search-component {
    display: flex;
    flex-wrap: wrap;
    .row {
      display: flex;
      align-items: center;
      margin-bottom: 16px;
    }
    .label {
      font-size: 14px;
      color: #333333;
      margin-right: 8px;
      font-weight: 400;
    }
    .keyword {
      margin-right: 32px;
    }
  }
  .search-btn {
    margin-left: 50px;
    display: flex;
    .reset-btn {
      background: #f2f3f5;
      border: 1px solid #f2f3f5;
      border-radius: 2px;
      color: #333333;
      padding: 5px 16px;
    }
    .submit-btn {
      background: #007aff;
      border: 1px solid #007aff;
      border-radius: 2px;
      padding: 5px 16px;
    }
    .icon-img {
      width: 16px;
      height: 16px;
      margin-right: 8px;
    }
  }
}
</style>
